﻿using DocBot.Objects;
using DocBot.Util;
using DocBot.Util.Collections;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

using System;
using System.IO;
using System.Windows.Input;
using System.Xml.Serialization;

namespace DocBot.ViewModel
{
	class CustomCommandViewModel : ViewModelBase
	{
		#region Members
		private static string configFileName = "CustomCommands.xml";
		private LocalDataBase _database = new LocalDataBase();
		ThreadSafeObservableCollection<CustomCommand> _items = new ThreadSafeObservableCollection<CustomCommand>();
		int _selectedItem = -1;
		#endregion

		#region Properties
		public ThreadSafeObservableCollection<CustomCommand> Items
		{
			get
			{
				return _items;
			}
			set
			{
				_items = value;
			}
		}
		#endregion

		#region Construction
		public CustomCommandViewModel()
		{
			if (IsInDesignMode)
			{
				LoadCustomCommands(configFileName);
				if (_items.Count == 0)
					for (int i = 0; i < 50; ++i)
					{
						_items.Add(new CustomCommand { Command = _database.GetRandomArtistName, Response = _database.GetRandomMessage });
					}
			}
			else
			{
				LoadCustomCommands(configFileName);
			}
		}

		~CustomCommandViewModel()
		{
			if (!IsInDesignMode)
				SaveCustomCommands(configFileName);
		}

		public int SelectedItem
		{
			get
			{
				return this._selectedItem;
			}
			set
			{
				if (value != this._selectedItem)
				{
					this._selectedItem = value;
				}
			}
		}

		#endregion

		#region Methods
		public void Add(String _command, String _response)
		{
			_items.Add(new CustomCommand { Command = _command, Response = _response });
		}

		public void LoadCustomCommands(String configFileName)
		{

			if (File.Exists(Settings.Instance.configFilePath + "\\" + configFileName))
			{
				XmlSerializer deserializer = new XmlSerializer(typeof(ThreadSafeObservableCollection<CustomCommand>));
				TextReader textReader = new StreamReader(Settings.Instance.configFilePath + "\\" + configFileName);
				ThreadSafeObservableCollection<CustomCommand> _object = (ThreadSafeObservableCollection<CustomCommand>)deserializer.Deserialize(textReader);
				textReader.Close();
				if (_object == null)
				{
					_object = new ThreadSafeObservableCollection<CustomCommand>();
				}
				_items = _object;
			}
		}

		private void SaveCustomCommands(String configFileName)
		{
			if (_items.Count > 0)
			{
				XmlSerializer s = new XmlSerializer(typeof(ThreadSafeObservableCollection<CustomCommand>));
				TextWriter writer = new StreamWriter(Settings.Instance.configFilePath + "\\" + configFileName);
				s.Serialize(writer, _items);
				writer.Close();
			}
		}
		#endregion

		#region Clear Items
		void ClearExecute()
		{
			if (_items == null)
				return;

			this._selectedItem = -1;
			_items.Clear();
		}

		bool CanClearExecute()
		{
			return true;
		}

		public ICommand ClearItems { get { return new RelayCommand(ClearExecute, CanClearExecute); } }
		#endregion

		#region RemoveItem
		void RemoveBlacklistNameExecute()
		{
			if (_items == null)
				return;
			if (_selectedItem > -1)
				if (_selectedItem < (_items.Count - 1))
			_items.Remove(_items[_selectedItem]);
		}

		bool CanRemoveBlacklistNameExecute()
		{
			return true;
		}

		public ICommand RemoveItem { get { return new RelayCommand(RemoveBlacklistNameExecute, CanRemoveBlacklistNameExecute); } }
		#endregion
	}
}
